#include "StdAfx.h"
#include "WidgetUtils.h"


WidgetUtils::WidgetUtils(void)
{
}

WidgetUtils::~WidgetUtils(void)
{
}

int WidgetUtils::GetCComboBoxCurSel(const CWnd* pWnd, const int widgetID)
{
	if (pWnd == nullptr || widgetID <= 0)
	{
		return -1;
	}
	CComboBox* pcbx = (CComboBox*)(pWnd->GetDlgItem(widgetID));
	if (pcbx == nullptr)
	{
		return -1;
	}
	int curSel = pcbx->GetCurSel();

	return curSel;
}

int WidgetUtils::GetCComboBoxCurSel(const CWnd* pWnd, const CComboBox* pcbx)
{
	if (pWnd == nullptr || pcbx == nullptr)
	{
		return -1;
	}
	int curSel = pcbx->GetCurSel();

	return curSel;
}

void WidgetUtils::comboboxSetCurSel(CWnd* pWnd, int cbxID, const int curSel)
{
	CComboBox* pCbx = (CComboBox*)(pWnd->GetDlgItem(cbxID));
	if (pCbx != nullptr)
	{
		comboboxSetCurSel(pWnd, pCbx, curSel);
	}
}

void WidgetUtils::comboboxSetCurSel(CWnd* pWnd, CComboBox* pCbx, const int curSel)
{
	pCbx->SetCurSel(curSel);
	UINT nId = pCbx->GetDlgCtrlID();
	pWnd->PostMessage(WM_COMMAND, MAKEWPARAM(nId, CBN_SELCHANGE), (LPARAM)pCbx->GetSafeHwnd());
}

void WidgetUtils::disabledDateTimeCtrl(CWnd* pWnd, const int dateTimeCtrlID)
{
	CDateTimeCtrl* pdateTimeCtrl = (CDateTimeCtrl*)pWnd->GetDlgItem(dateTimeCtrlID);
	if (pdateTimeCtrl != nullptr)
	{
		::SendMessage(pdateTimeCtrl->m_hWnd, (UINT)DTM_SETSYSTEMTIME, GDT_NONE, NULL);
	}	
}

void WidgetUtils::showWindowToCenter(CWnd* pWnd)
{
	CRect wndRect;
	pWnd->GetClientRect(&wndRect);
	RECT rt;
	SystemParametersInfo(SPI_GETWORKAREA, 0, (PVOID)&rt, 0);
	int centerX = (rt.right - wndRect.right) / 2;
	int centerY = (rt.bottom - wndRect.bottom) / 2;
	pWnd->MoveWindow(centerX, centerY, wndRect.Width(), wndRect.Height());
}

long WidgetUtils::getDateTimeCtrlTime(CWnd* pWnd, const int dateTimeCtrlID)
{
	CDateTimeCtrl* pdateTimeCtrl = (CDateTimeCtrl*)pWnd->GetDlgItem(dateTimeCtrlID);
	if (pdateTimeCtrl != nullptr)
	{
		CTime tm;	
		DWORD dwResult = pdateTimeCtrl->GetTime(tm);
		if (dwResult == GDT_VALID)
		{
			long submitTm = tm.GetTime();
			return submitTm;
		}
	}

	return 0;
}

bool WidgetUtils::getSelectColor(COLORREF& defaultColor, COLORREF& selColor)
{
	CColorDialog dlg;
	dlg.m_cc.Flags |= CC_RGBINIT | CC_FULLOPEN;
	dlg.m_cc.rgbResult = defaultColor;
	if(IDOK == dlg.DoModal())
	{
		selColor = dlg.m_cc.rgbResult;
		return true;
	}
	else
	{
		return false;
	}
}

